package com.huisou.basedataservice;

import com.huisou.domain.Profession;
import com.huisou.orm.HibernateDao;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class ProfessionDao extends HibernateDao<Profession, Integer> {
	protected Logger						log						= LoggerFactory.getLogger(getClass());
	/**
	 * 缓存功能放在DAO层处理，减轻业务层压力
	 */
	private static final String				SALE_CATES_CACHE_NAME	= "saleCategoriesCache";																				// 产品供应类别
	private static final String				BUY_CATES_CACHE_NAME	= "buyCategoriesCache";																				// 产品供应类别
																																											
	private final String					tophql					= "select new Profession(id,code,name) from Profession where parentId is null order by orderNo asc ";
	private final String					hql						= "select new Profession(id,code,name) from Profession where parentId=? order by orderNo asc ";
	
	@Resource
	RedisTemplate<String, List<Profession>>	redisListTemplate;
	
	@Resource
	private StringRedisTemplate				stringRedisTemplate;
	
	/**
	 * 获取行业顶级类别列表
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Profession> getTopCateList() {
		String key = RedisConstants.KEY_PROFESSION_TOP;
		List<Profession> listProfession = null;
		try {
			listProfession = redisListTemplate.opsForValue().get(key);
		}
		catch (Exception e) {
			log.error("获取顶级行业类别列表出错.");
		}
		if (listProfession == null) {
			try {
				listProfession = find(tophql);
				if (listProfession != null && listProfession.size() > 0) {
					redisListTemplate.opsForValue().set(key, listProfession);
				}
			}
			catch (DataAccessException e) {
				log.error("获取顶级行业类别列表出错.");
			}
		}
		return listProfession;
	}
	
	/**
	 * 根据父类ID，获取行业类别列表
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Profession> getListByParentId(Integer parentId) {
		String key = RedisConstants.KEY_PROFESSION_LIST + "_" + parentId;
		List<Profession> listProfession = null;
		try {
			listProfession = redisListTemplate.opsForValue().get(key);
		}
		catch (Exception e) {
			log.error("获取顶级行业类别列表出错.");
		}
		
		if (listProfession == null) {
			try {
				listProfession = find(hql, parentId);
				if (listProfession != null && listProfession.size() > 0) {
					redisListTemplate.opsForValue().set(key, listProfession);
				}
			}
			catch (DataAccessException e) {
				log.error("获取顶级行业类别列表出错.");
			}
		}
		return listProfession;
	}
	
	/**
	 * 获取产品供应类别
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Profession> getChilds(Integer parentId) {
		return getChilds(parentId, 2, SALE_CATES_CACHE_NAME);
	}
	
	/**
	 * 获取产品求购类别
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Profession> getBuyCates(Integer parentId) {
		return getChilds(parentId, 2, BUY_CATES_CACHE_NAME);
	}
	
	/**
	 * 第一次从数据库中获得子分类列表， 之后都从内存中读取
	 * 
	 * @param parentId
	 * @return
	 */
	public List<Profession> getChilds(Integer parentId, int posInx, String cacheName) {
		String key = RedisConstants.KEY_PROFESSION_CHILDS + "_" + cacheName + "_" + parentId + "_" + posInx;
		List<Profession> listProfession = null;
		try {
			listProfession = redisListTemplate.opsForValue().get(key);
		}
		catch (Exception e) {
			log.error("获取顶级行业类别列表出错.");
		}
		if (listProfession == null) {
			try {
				listProfession = this.find("select new Profession(id,name) from Profession where isDeleted='0' and parentId=? and substr(typeRex,?,1)='1' order by orderNo ", parentId, posInx);
				if (listProfession != null && listProfession.size() > 0) {
					redisListTemplate.opsForValue().set(key, listProfession);
				}
			}
			catch (DataAccessException e) {
				log.error("获取顶级行业类别列表出错.");
			}
		}
		return listProfession;
	}
	
	public String getNameByCode(String code) {
		String key = RedisConstants.KEY_PROFESSION_SIGNLE_NAME + "_" + code;
		String professionName = "";
		try {
			professionName = stringRedisTemplate.opsForValue().get(key);
		}
		catch (Exception e) {
			log.error("获取行业类别列表出错.");
		}
		if (StringUtils.isBlank(professionName)) {
			try {
				professionName = this.findUniqueBy("code", code).getName();
				stringRedisTemplate.opsForValue().set(key, professionName);
			}
			catch (DataAccessException e) {
				log.error("获取顶级行业类别列表出错.");
			}
		}
		return professionName;
	}
}
